from django.db import models
from django.contrib.auth.models import User
from tagging.fields import TagField
from tagging.models import Tag

from ceemtu.cetcore.models import FileEntry
import datetime

from mp3 import winamp_genres

MUSIC_GENRES = []
for genre in winamp_genres:
    MUSIC_GENRES.append((genre, genre))

class Song(models.Model):
    """
    
    """
    file_entry = models.ForeignKey(FileEntry, editable = False)
    title = models.CharField(max_length = 200, blank = True, null = True)
    artist = models.CharField(max_length = 200, blank = True, null = True)
    album = models.CharField(max_length = 200, blank = True, null = True)
    release_year = models.CharField(max_length=4, blank = True, null = True)
    bitrate = models.CharField(max_length = 10, editable = False)
    genre = models.CharField(max_length = 20, choices = MUSIC_GENRES, null = True, blank = True)
    minutes = models.IntegerField(editable = False)
    seconds = models.IntegerField(editable = False)
    date_added = models.DateTimeField(default=datetime.datetime.today(), editable = False)
    cover_image_hash = models.CharField(max_length = 128, null = True, blank = True, editable = False)
    tags = models.ManyToManyField(Tag, null = True, blank = True)

    class Meta:
        ordering = ['-date_added', 'artist', 'album', 'title']
        verbose_name = "Song"
        verbose_name_plural = "Songs"

    def __unicode__(self):
        if not self.title and self.artist and self.album:
            return self.file_entry.filename
        else:
            return '"%s" by %s, "%s"' % (self.title, self.artist, self.album)

class Playlist(models.Model):
    """
    
    """
    user = models.ForeignKey(User)
    title = models.CharField(max_length = 200, blank = True, null = True)
    genre = models.CharField(max_length = 20, choices = MUSIC_GENRES)
    date_added = models.DateTimeField(default=datetime.datetime.now, editable = False)
    songs = models.ManyToManyField(Song)

    class Meta:
        ordering = ['-date_added', 'title']
        verbose_name = "Playlist"
        verbose_name_plural = "Playlists"

    def __unicode__(self):
            return self.title